<?php
namespace BoxEdu\Logic;
class SubjectLogic extends \HomeController {
	public function _initialize() {
		parent::_initialize();
		//	 $this->user_id=session("user_id");
	}

	/****是否开通课程***/
	public function has_buy($subject_id, $user_id) {
		$data = BoxModel("edu_subject")->rootSubject($subject_id);
		$subject_id = $data['subjectid'];
		$where1 = array(
			'user_id' => $user_id ? $user_id : $this->user_id,
			'subjectid' => $subject_id,
			'type' => 1,
			'status' => array('in', "2,4"),
		);
		//	dump($where1);exit();
		$r1 = BoxModel("edu_subject_order")->where($where1)->find();
		// $r2 = BoxModel("edu_subject_order")->where($where2)->find();
		if ($r1) {
			return $this->return_array(0, "正式开通课程");
		} else {
			return $this->return_array('1', "没有购买该课程");
		}
	}

	/*****是否开通线下课程**/
	public function has_offline($subject_id, $user_id) {
		//dump($subject_id);
		$data = BoxModel("edu_subject")->rootSubject($subject_id);
		$subject_id = $data['subjectid'];
		//dump($subject_id);exit();
		$where2 = " user_id = '$user_id' and subjectid=' $subject_id' and status in (2,4)";
		$r1 = BoxModel("edu_subject_order")->where($where2)->find();
		//$user_info=BoxModel("edu_user")->where(array('user_id'=>$user_id))->find();
		//	$subjectids=json_decode($user_info['subjectids'],true);
		//	dump($subject_id);
		//dump($subjectids);
		//dump($r1);exit();
		if (I('dubug')) {
			dump($where2);
			dump($r1);
		}
		if ($r1['offline_subject_ids']) {
			return $this->return_array(0, "已开通线下课程");
		} else {
			return $this->return_array('1', "没有开通线下课程");
		}

	}

	/****看视频获得学分***/
	public function open_video($parm) {
		$knowsid = $parm['knowsid'];
		$subjectid = $parm['subjectid'];
		$knows_data = BoxModel("edu_knows")->load($knowsid);
		$has_buy = $this->has_buy($subjectid);
		$key_name = md5($this->user_id . "_" . $subjectid . "_" . $knowsid . "_xuebazi_from_open_video");
		if ($has_buy['error'] == 0) {
			$add_to_log = BoxModel("system_log")->where("key_name='$key_name'")->find();
			$row = array(
				'num' => $knows_data['xuebazi'],
				//'remark' => "在线观看视频：【" . $knows_data['knows'] . "】测试获得" . $knows_data['xuebazi'] . "学分",
				'subjectid' => $subjectid,
				'key_name' => $key_name,
				'url' => HomeUrl . U('Subject/lesson_detail', array('knowsid' => $knowsid, 'subjectid' => $subjectid)),
				'title' => $knows_data['knows'],
				'event' => '00006',
			);
			if (!$add_to_log) {
				$r = Logic("BoxEdu", "User")->add_xuebazi($row);
			}
		}
	}

	/********一个学校线下，可上的线下课程**/
	public function get_offline_subject($param) {
		$offline_subject = BoxModel("edu_classrooms_subject")->get($param);
		foreach ($offline_subject as $one) {
			$offline_subject_ids[$one['offline_subject_id']] = $one['offline_subject_id'];
		}
		$offline_subject_ids = implode(",", $offline_subject_ids);
		$data = BoxModel("edu_offline_subject")->get("id in ($offline_subject_ids)");
		return $data;
	}

	/********一个学校线下，可上的线下课程**/
	public function get_offline_subject_price($param) {
		$model = BoxModel("edu_offline_subject_price");
		$where = array(
			'school_id' => $param['school_id'],
			'offline_subject_id' => $param['subjectid'],
		);
		$data = $model->where($where)->find();
		$subjectid = $data['offline_subject_id'];
		$subject_info = BoxModel("edu_subject")->where(array('subjectid' => $subjectid))->find();
		$data['examination_fee'] = $subject_info['examination_fee'];
		//$data['price']=$data['price']?$data['price']:0;
		return $data;
	}

}